
    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace CSharpPro._1103;

/// <summary>
/// 用户服务实现类（核心业务逻辑）
/// </summary>
public class UserService : IUserService
{
    /// <summary>
    /// 泛型集合（List<T>）存储注册用户信息
    /// </summary>
    private readonly List<User> _registeredUsers = new List<User>();

    /// <summary>
    /// 当前登录用户
    /// </summary>
    public User CurrentUser { get; private set; }

    /// <summary>
    /// 注册实现
    /// </summary>
    public string Register(string username, string password, string confirmPassword)
    {
        
        if (string.IsNullOrWhiteSpace(username)) return "用户名不可为空";
        if (string.IsNullOrWhiteSpace(password)) return "密码不可为空";
        if (string.IsNullOrWhiteSpace(confirmPassword)) return "确认密码不可为空";

        
        if (username.Length < 3 || username.Length > 15)
            return "用户名长度必须为 3 - 15 个字符";
        if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
            return "用户名只能包含字母、数字、下划线";

        
        if (password.Length < 6 || password.Length > 20)
            return "密码长度必须为 6 - 20 个字符";
        if (!Regex.IsMatch(password, @"[A-Z]") || !Regex.IsMatch(password, @"[a-z]") || !Regex.IsMatch(password, @"[0-9]"))
            return "密码必须包含至少 1 个大写字母、1 个小写字母和 1 个数字";
        if (password != confirmPassword)
            return "密码与确认密码不一致";

        
        bool isUsernameExists = false;
        foreach (var user in _registeredUsers)
        {
            if (user.Username == username)
            {
                isUsernameExists = true;
                break;
            }
        }
        if (isUsernameExists)
            return "用户名已存在";

        
        _registeredUsers.Add(new User(username, password));
        return "注册成功";
    }

    /// <summary>
    /// 登录实现
    /// </summary>
    public string Login(string username, string password)
    {
        
        if (string.IsNullOrWhiteSpace(username)) return "用户名不可为空";
        if (string.IsNullOrWhiteSpace(password)) return "密码不可为空";

        
        User targetUser = null;
        foreach (var user in _registeredUsers)
        {
            if (user.Username == username)
            {
                targetUser = user;
                break;
            }
        }

        
        if (targetUser == null) return "用户名不存在";
        if (targetUser.Password != password) return "密码错误";

        // 4. 登录成功：设置当前登录用户
        CurrentUser = targetUser;
        return "登录成功";
    }

    /// <summary>
    /// 退出登录
    /// </summary>
    public void Logout()
    {
        CurrentUser = null;
    }

    /// <summary>
    /// （可选）测试用：删除用户（演示删除时使用for循环）
    /// </summary>
    /// <param name="username">要删除的用户名</param>
    /// <returns>删除结果</returns>
    public string DeleteUser(string username)
    {
        if (string.IsNullOrWhiteSpace(username)) return "用户名不可为空";

        
        for (int i = 0; i < _registeredUsers.Count; i++)
        {
            if (_registeredUsers[i].Username == username)
            {
                _registeredUsers.RemoveAt(i);
                
                if (CurrentUser?.Username == username)
                {
                    Logout();
                }
                return $"用户 {username} 删除成功";
            }
        }

        return "用户不存在";
    }
}
